import { ResourceRoleEntity } from 'src/api/resource/entities/resource.role.entity';
import { SharedEntity } from 'src/api/shared/entities/shared.entity';
import { UserRoleEntity } from 'src/api/user/entities/user.role.entity';
import { Column, Entity, OneToMany } from 'typeorm';

@Entity('role')
export class RoleEntity extends SharedEntity {
  @Column({
    type: 'varchar',
    name: 'name',
    nullable: false,
    length: 50,
    comment: '角色名称',
  })
  name: string;

  @Column({
    type: 'varchar',
    name: 'description',
    nullable: true,
    comment: '角色描述',
  })
  description: string;

  @Column({
    type: 'tinyint',
    nullable: true,
    default: 0,
    name: 'is_default',
    comment: '针对后期提供注册用,1表示默认角色,0表示非默认角色',
  })
  isDefault: number;

  // 一个角色对应多个用户
  @OneToMany(() => UserRoleEntity, (userRole) => userRole.role)
  userRoles: UserRoleEntity[];

  // 一个角色对应多个资源
  @OneToMany(() => ResourceRoleEntity, (resourceRole) => resourceRole.role, { cascade: true })
  roleResource: ResourceRoleEntity[];
}
